/**
 * <p>文件名:		IntegrateTest.java</p>
 * <p>版权:		CTFO.COM</p>
 * <p>公司:		CTFO.COM</p>
 * @author		宋晓达 ٩(·̮̃·̃)۶ (songxiaoda@ctfo.com songxiaoda@odare.info)
 */
package com.caits.lbs.framework.services.hessian.client;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;

import javax.servlet.ServletException;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.caits.lbs.framework.Constants;
import com.caits.lbs.framework.log.CommonLogFactory;
import com.caits.lbs.framework.services.hessian.client.session.HessianProxyServiceFactoryBean;
import com.caits.lbs.framework.services.hessian.server.IHessianBaseService;
import com.caits.lbs.framework.utils.Dom4jXmlUtil;
import com.common.ajax.server.IRequest;
import com.common.ajax.server.RequestParser;
import com.common.ajax.server.SessionMap;

/**
 * <p>
 * IntegrateTest
 * </p>
 * <p>
 * 调用远程hessian服务的客户端实现类
 * </p>
 * 
 * @author 		宋晓达 ٩(·̮̃·̃)۶ (songxiaoda@ctfo.com)
 * @version 1.0.0
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>1.0.0</td>
 *          <td>创建类, 并添加三类基本测试</td>
 *          <td>sxd</td>
 *          <td>2011-01-05 15:00:00</td>
 *          </tr>
 *          <tr>
 *          <td></td>
 *          <td></td>
 *          <td></td>
 *          <td></td>
 *          </tr>
 *          </table>
 */
public class RemoteHessianRequestParser implements IRequest {
	
	/** 日志记录类 */
	public Logger log = CommonLogFactory.getLog();
	/** 远程调用接口通用类 */
	private IHessianBaseService remoteBean = null;
	
	/** 系统常量类 */
	protected Constants constant;
	
	/**
	 * 初始化工作 
	 * @throws ServletException
	 */
	public void init() throws ServletException {

//		remoteBean = (HessianProxyServiceFactoryBean) SpringBUtils.getBean("remoteService");
		log.info("处理器运行在Hessian模式，Class:" + remoteBean.getClass().getCanonicalName());
		
	}

	/**
	 * 设置属性 remoteBean 的值，HessianProxyServiceFactoryBean类型
	 * @param remoteBean the remoteBean to set
	 */
	public void setRemoteBean(IHessianBaseService remoteBean) {
		this.remoteBean = remoteBean;
	}
	/**
	 * 获取属性remoteBean的值
	 * @return the remoteBean : HessianProxyServiceFactoryBean
	 */
	public IHessianBaseService getRemoteBean() {
		return remoteBean;
	}
	/* 
	 * (non-Javadoc)
	 * @see com.common.ajax.server.IRequest#request(org.jdom.Document, com.common.ajax.server.SessionMap)
	 */
	@Override
	public Document request(Document doc, SessionMap session)
			throws SecurityException, NoSuchMethodException,
			IllegalAccessException, InvocationTargetException, SQLException,
			RuntimeException {
		int result = IRequest.ERROR_SUCCEED;
		String className = "no-parse-server";
		Document return_obj = null;
//		String return_obj = null;
		try {
			//暂时不采用压缩方式
			return_obj = getRemoteBean().docService("cookie",doc,0);
//			return_obj = getRemoteBean().xmlService("cookie",XmlUtil.documentToString(doc),0);
			log.debug("hessian call result:" + return_obj.toString().length());
		} catch (Exception ex1) {
			result = IRequest.ERROR_INSTANTIATION;
			return RequestParser.createError(className, result,
					IRequest.ERROR_INSTANTIATION_DESC);
		}
		return return_obj;
	}
	
	
	
	@Override
	public String request(String xmlStr, SessionMap session)
			throws SecurityException, NoSuchMethodException,
			IllegalAccessException, InvocationTargetException, SQLException,
			RuntimeException {
		int result = IRequest.ERROR_SUCCEED;
		String className = "no-parse-server";
		String return_obj = null;

		try {
			//暂时不采用压缩方式
			return_obj = getRemoteBean().xmlService("cookie",xmlStr,0);
			log.debug("hessian call result:" + return_obj.substring(0,Math.min(Constants.max_log_output, return_obj.length())));
		} catch (Exception ex1) {
			result = IRequest.ERROR_INSTANTIATION;
			return Dom4jXmlUtil.documentToString(RequestParser.createError(className, result,
					IRequest.ERROR_INSTANTIATION_DESC));
		}
		return return_obj;
	}

	/**
	 * 获取变量constant的值
	 * @return the constant 数据类型 : Constants
	 */
	public Constants getConstant() {
		return constant;
	}


	/**
	 * 设置变量 constant 的值，数据类型：Constants
	 * @param constant the constant to set
	 */
	public void setConstant(Constants constant) {
		this.constant = constant;
	}


	/**
	 * FIXME 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
			System.out.println("Start using spring hessian proxy. ");
			
			//采用spring自带的hession包  不能实现SESSION 的传递  stateless
			ApplicationContext ctx_basic=new ClassPathXmlApplicationContext("applicationContext*.xml");
			
			//集成扩展工厂类采用扩展的，推荐使用   stateful, 能传递 session 
			
			HessianProxyServiceFactoryBean bean = (HessianProxyServiceFactoryBean) ctx_basic.getBean("remoteService");
			RemoteHessianRequestParser call = new RemoteHessianRequestParser();
			
	
			
			
			
			
			
		}

}
